package com.sunny.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunny.common.ServerResponse;
import com.sunny.dao.UserMapper;
import com.sunny.pojo.User;
import com.sunny.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by yubao.jin on 2018/3/7.
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse<User> login(String domainAccount) {
        User user = userMapper.checkDomainAccount(domainAccount);
        if(user == null) {
            return ServerResponse.createByErrorMessage("域账号不存在");
        }

        return ServerResponse.createBySuccessMessage("登录成功", user);
    }

    @Override
    public ServerResponse<List<User>> userList(String username, String role, String state) {
        List<User> list = userMapper.selectUser(username, role, state);
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse<PageInfo<User>> userList(int pageNum, int pageSize, String username, String role, String state) {
        if(username != null && role != null && state != null) {
            username = username.equals("all") ? null : username;
            role = role.equals("all") ? null : role;
            state = state.equals("all") ? null : state;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectUser(username, role, state);
        PageInfo<User> pageInfo = new PageInfo(userList);
        pageInfo.setList(userList);
        System.out.println(pageInfo);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<User> selectUserById(int userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return ServerResponse.createBySuccess(user);
    }

    @Override
    public ServerResponse updateUser(String id, String role, String username, String phoneNumber) {
        User userRecord = new User();
        userRecord.setId(Integer.parseInt(id));
        if(username != null) {
            userRecord.setUsername(username);
        }
        if(role != null) {
            userRecord.setRole(Integer.parseInt(role));
        }
        if(phoneNumber != null) {
            userRecord.setPhoneNumber(phoneNumber);
        }

        int resultCount = userMapper.updateByPrimaryKeySelective(userRecord);
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("更新失败");
        }

        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @Override
    public ServerResponse disableUser(String userId) {
        User userRecord = new User();
        userRecord.setId(Integer.parseInt(userId));
        userRecord.setState(0);
        int resultCount = userMapper.updateByPrimaryKeySelective(userRecord);
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("禁用失败");
        }

        return ServerResponse.createBySuccessMessage("禁用成功");
    }

    @Override
    public ServerResponse enableUser(String userId) {
        User userRecord = new User();
        userRecord.setId(Integer.parseInt(userId));
        userRecord.setState(1);
        int resultCount = userMapper.updateByPrimaryKeySelective(userRecord);
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("启用失败");
        }

        return ServerResponse.createBySuccessMessage("启用成功");
    }

    @Override
    public ServerResponse insertUser(String state, String role, String username, String phoneNumber, String domainAccount){
        User userRecord = new User();
        if(username != null) {
            userRecord.setUsername(username);
        }
        if(role != null) {
            userRecord.setRole(Integer.parseInt(role));
        }
        if(phoneNumber != null) {
            userRecord.setPhoneNumber(phoneNumber);
        }
        if(state != null) {
            userRecord.setState(Integer.parseInt(state));
        }
        if(domainAccount != null) {
            userRecord.setDomainAccount(domainAccount);
        }

        int resultCount = userMapper.insertSelective(userRecord);

        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("注册失败");
        }

        return ServerResponse.createBySuccessMessage("注册成功");
    }
}
